import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STColumn, STComponent, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzModalService } from 'ng-zorro-antd/modal';
import { ChannelSalesService } from '../../services/channel-sales.service';
import { ParterChannelSalesEditComponent } from '../edit/edit.component';

@Component({
  selector: 'app-parter-channel-sales-list',
  templateUrl: './list.component.html'
})
export class ParterChannelSalesListComponent implements OnInit {
  schema: SFSchema = {};
  columns!: STColumn[];
  ui!: SFUISchema;
  @ViewChild('st', { static: false })
  st!: STComponent;
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  spuStatus = '1';

  data = [{ name1: 1111 }]
  constructor(
    public router: Router,
    public service: ChannelSalesService,
    private modalService: NzModalService
  ) { }

  /**
   * 查询参数
   */
  get reqParams() {
    return { ...this.sf?.value };
  }
  ngOnInit() {
    this.initSF();
    this.initST();
  }

  initSF() {
    this.schema = {
      properties: {
        name: {
          type: 'string',
          title: '销售渠道姓名'
        },
        telephone: {
          type: 'string',
          title: '手机号'
        },
      }
    };
    this.ui = {
      '*': {
        grid: { span: 8, gutter: 4 }
      }
    };
  }

  initST() {
    this.columns = [
      {
        title: '销售渠道姓名',
        index: 'name',
        width: '180px'
      },
      {
        title: '手机号',
        index: 'telephone',
        width: '150px'
      },
      {
        title: '关联OA员工姓名',
        index: 'empName',
        width: '150px'
      },
      {
        title: '关联OA员工手机号',
        index: 'mobile',
        width: '180px'
      },
      {
        title: '所属组织',
        index: 'organLable',
        width: '300px'
      },
      {
        title: '职级',
        index: 'station',
        width: '150px'
      },
      {
        title: '等级',
        index: 'postLevel',
        width: '150px'
      },
      {
        title: '省市',
        index: 'residencePlace',
        width: '150px'
      },
      {
        title: '邀请码',
        index: 'inviteCode',
        width: '150px'
      },
      {
        title: '状态',
        className: 'text-center',
        index: 'stateLocked',
        type: 'badge',
        width: '150px',
        badge: {
          true: { text: '冻结', color: 'error' },
          false: { text: '正常', color: 'success' }
        },
      },
      {
        title: '操作',
        className: 'text-center',
        width: '120px',
        fixed: 'right',
        buttons: [
          {
            text: '编辑',
            acl: { ability: ['channelSales-edit'] },
            iif: (item) => {return item.stateLocked == false},
            click: (_record, _modal, _instance) => this.edit(_record),
          },
          {
            text: '冻结',
            acl: { ability: ['channelSales-frozen'] },
            iif: (item) => {return item.stateLocked == false},
            click: (_record, _modal, _instance) => this.stop(_record),
          },
          {
            text: '启用',
            acl: { ability: ['channelSales-open'] },
            iif: (item) => {return item.stateLocked == true},
            click: (_record, _modal, _instance) => this.open(_record),
          },
        ]
      }
    ];
  }

  add() {
    const modalRef = this.modalService.create({
      nzWidth: 600,
      nzTitle: '新增',
      nzContent: ParterChannelSalesEditComponent,
      nzComponentParams: { sts: 'add' }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }

  // 编辑
  edit(record: STData) {
    const modalRef = this.modalService.create({
      nzWidth: 600,
      nzTitle: '编辑',
      nzContent: ParterChannelSalesEditComponent,
      nzComponentParams: { i: record }
    });
    modalRef.afterClose.subscribe(res => {
      if (res) {
        this.st.reload();
      }
    });
  }
  stop(record: STData) {
    if (!record.stateLocked) {
      const params = {
        id: record.id
      }
      this.modalService.confirm({
        nzTitle: '<i>冻结确认</i>',
        nzContent: `<b>确定冻结该账号吗？</br>`,
        nzOnOk: () =>
          this.service.request(this.service.$api_frozenChannelSales, params).subscribe(res => {
            if (res) {
              this.service.msgSrv.success('冻结成功！');
              this.st.reload();
            }
          })
      });
    }
  }
  open(record: STData) {
    if (record.stateLocked) {
      const params = {
        id: record.id
      }
      this.modalService.confirm({
        nzTitle: '<i>启用确认</i>',
        nzContent: `<b>确定启用该账号吗？</br>`,
        nzOnOk: () =>
          this.service.request(this.service.$api_activeChannelSales, params).subscribe(res => {
            if (res) {
              this.service.msgSrv.success('启用成功！');
              this.st.reload();
            }
          })
      });
    }
  }

  /**
   * 重置表单
   */
  resetSF() {
    this.sf.reset();
    this.st.load(1);
  }


}
